package com.ly.web.controller.king;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.CommonUtil;
import com.ly.heart.domain.dto.KingMaterialDetailComputeAmountDto;
import com.ly.heart.domain.vo.KingMaterialDetailComputeAmountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Wuzh
 * @Description:进销存中所有物料明细： 订单数量 含税单价    单价	税率	金额	 税额 	 价税合计
 * 等计算规则；
 * 前端需提供：数量、税率、价税合计三个值，返回其他的所有值
 * @Date Created in 23:39 2024-07-27
 * @return
 */
@Api (value = "detailAmount", tags = "计算物料明细行数据")
@RestController
@RequestMapping ( "/heart/detailAmount" )
public class KingMaterialDetailComponent {


    /**
     * 计算规则：
     *
     * @param kingMaterialDetailComputeAmountDto
     * @return
     */
    @GetMapping ( "compute" )
    @ApiOperation ("计算接口")
    public AjaxResult compute ( KingMaterialDetailComputeAmountDto kingMaterialDetailComputeAmountDto ) {
        if ( StringUtils.isBlank ( kingMaterialDetailComputeAmountDto.getNumber () ) ) {
            throw new CustomException ( "数量不能为空！" );
        }
        if ( StringUtils.isBlank ( kingMaterialDetailComputeAmountDto.getTaxRate () ) ) {
            throw new CustomException ( "税率不能为空！" );
        }
        if ( StringUtils.isBlank ( kingMaterialDetailComputeAmountDto.getTotalTaxAmount () ) ) {
            throw new CustomException ( "价税合计不能为空！" );
        }
        if ( StringUtils.isBlank ( kingMaterialDetailComputeAmountDto.getRate () ) ) {
            throw new CustomException ( "汇率不能为空！" );
        }
        //if ( StringUtils.isNotBlank ( kingMaterialDetailComputeAmountDto.getDiscountRate () ) && Double.parseDouble ( kingMaterialDetailComputeAmountDto.getDiscountRate () ) >= 100 ) {
            //throw new CustomException ( "折扣率不能大于等于100%！" );
        //}
        /**
         * 计算，其中单价和含税单价返回保留10位小数点
         * 金额、税额、价税合计保留两位小数点
         * 1、含税单价=价税合计 / 数量
         * 2、单价=含税单价 / (1+税率)
         * 3、金额=数量*单价
         * 4、税额=价税合计-金额
         * 5、金额(本位币)=金额*汇率
         * 6、税额(本位币)=税额*汇率
         * 7、价税合计(本位币)=价税合计*汇率
         *
         *  <p>
         * 如果输入了折扣率：
         * 金额=原来金额-(原来金额*折扣率)
         * 税额=原来税额-(原来税额*折扣率)
         * 价税合计=原来价税合计-(原来价税合计*折扣率)
         * </p>
         *
         */
        KingMaterialDetailComputeAmountVo vo = new KingMaterialDetailComputeAmountVo ();
        if ( StringUtils.isBlank ( kingMaterialDetailComputeAmountDto.getDiscountRate () ) ) {
            //税率=13%，转成小数
            String taxRate = kingMaterialDetailComputeAmountDto.getTaxRate ();
            kingMaterialDetailComputeAmountDto.setTaxRate ( CommonUtil.divisionStr ( taxRate, "100", 2 ) );
            vo.setTaxUnitPrice ( CommonUtil.divisionStr ( kingMaterialDetailComputeAmountDto.getTotalTaxAmount (), kingMaterialDetailComputeAmountDto.getNumber (), 10 ) ).
                    setPrice ( CommonUtil.divisionStr ( vo.getTaxUnitPrice (), String.valueOf ( ( 1.0 + Double.parseDouble ( kingMaterialDetailComputeAmountDto.getTaxRate () ) ) ), 10 ) ).
                    setAmount ( CommonUtil.rideStr ( kingMaterialDetailComputeAmountDto.getNumber (), vo.getPrice (), 2 ) ).
                    setTaxAmount ( CommonUtil.subStr ( kingMaterialDetailComputeAmountDto.getTotalTaxAmount (), vo.getAmount (), 2 ) ).
                    setAmountCurrency ( CommonUtil.rideStr ( vo.getAmount (), kingMaterialDetailComputeAmountDto.getRate (), 2 ) ).
                    setTaxAmountCurrency ( CommonUtil.rideStr ( vo.getTaxAmount (), kingMaterialDetailComputeAmountDto.getRate (), 2 ) ).
                    setTotalTaxAmountCurrency ( CommonUtil.rideStr ( kingMaterialDetailComputeAmountDto.getTotalTaxAmount (), kingMaterialDetailComputeAmountDto.getRate (), 2 ) );
        } else {
            // 输入了折扣率

        }
        return AjaxResult.success ( vo );
    }

}
